var lng_k_email_inquiry = '';
var str_User_id = '';
var pass_doesnt_match_msg = '';

function win_onload() {
    if ( (lng_k_email_inquiry != "" && lng_k_email_inquiry != "0") && (str_User_id != "" && str_User_id != "0")) {
        document.getElementById('ch_pw_link').click();
        pwdStrength(document.getElementById('ch_pw_1').value);
        document.getElementById('hide_pw').checked = true;
        hide_pw(true);
    }
}

function setInquiry() {
    var params = 'action=setInquiry';
    dhtmlxAjax.post("change_pw.asp", params, function(loader) {
        var resp = loader.xmlDoc.responseText;
        if (resp.substr(0, 1) == '{') {
            var json = eval("(" + resp + ")");
            if (json && typeof json === 'object') {
                if (json.status == 'error') {
                    alert("Error: " + json.msg);
                } else {
                    if (json.msg != '' && json.msg != undefined) {
                        document.getElementById('lost_pw_msg_text').innerHTML = json.msg;
                        lost_pw_msg.update(135);
                        $('#lost_pw_msg_b').click();
                    } else {
                        if (json.inquiry != '' && json.user_id != '') {
                            if (json.iu != '0' && json.ig != '0') {
                                lng_k_email_inquiry = json.inquiry;
                                str_User_id = json.user_id;
                                win_onload();
                            } else {
                                window.location.href = "session_end.asp";
                            }
                        }
                    }
                }
            } else {
                alert('Unknown request error occured');
            }
        } else {
            alert('Error requesting the data. Error: ' + loader.xmlDoc.responseText);
        }
    });
}

function hide_pw(hide) {
    document.getElementById('ch_pw_1').type = (hide ? "password" : "text");
    document.getElementById('ch_pw_2').type = (hide ? "password" : "text");
}
function pwdStrength(password) {
    var color, bgColor;
//    desc[0] = "Very Weak";
//    desc[1] = "Weak";
//    desc[2] = "Average";
//    desc[3] = "Strong";
//    desc[4] = "Secure";
    bgColor = new Array(
     '#F93535',
     '#F9D2D2',
     '#ADADAD',
     '#1184D3',
     '#13A038'
     );
    color = new Array(
     '#FFFFFF',
     '#FF0000',
     '#000000',
     '#FFFFFF',
     '#FFFFFF'
     );
    var score = 0;
    var pw_chars = password.split('');
    var _allowed_chars = allowed_chars.split('');
    var allowed = true;
    for (var i = 0; i < pw_chars.length; i++) {
        if (!in_array(pw_chars[i], _allowed_chars)) {
            allowed = false;
        }
    }
    if (password.length > 7) {
        if (password.length < 26) {
            var a = 0, i;
            for (i = 0; i < must_have_groups.length; i++) {
                if (password.search(new RegExp("[" + must_have_groups[i] + "]")) != -1) {
                    a++;
                }
            }
            if (a < 3) {
                document.getElementById("password_strengh").innerHTML = err_msgs[3];
                document.getElementById("password_strengh").style.backgroundColor = bgColor[0];
                document.getElementById("password_strengh").style.color = color[0];
                document.getElementById("pw_ch_button").disabled = true;
            } else {
                score = a - 1;
                if (password.length > 9) {
                    score++;
                }
                if (password.length > 12)
                    score++;
                score = score > 4 ? 4 : score;
                document.getElementById("password_strengh").innerHTML = desc[score];
                document.getElementById("password_strengh").style.backgroundColor = bgColor[score];
                document.getElementById("password_strengh").style.color = color[score];
                document.getElementById("pw_ch_button").disabled = false;
            }

        } else {
            document.getElementById("password_strengh").innerHTML = err_msgs[1];
            document.getElementById("password_strengh").style.backgroundColor = bgColor[0];
            document.getElementById("password_strengh").style.color = color[0];
            document.getElementById("pw_ch_button").disabled = true;
        }
    } else {
        document.getElementById("password_strengh").innerHTML = err_msgs[0];
        document.getElementById("password_strengh").style.backgroundColor = bgColor[0];
        document.getElementById("password_strengh").style.color = color[0];
        document.getElementById("pw_ch_button").disabled = true;
    }
    if (!allowed) {
        document.getElementById("password_strengh").innerHTML = err_msgs[2];
        document.getElementById("password_strengh").style.backgroundColor = bgColor[0];
        document.getElementById("password_strengh").style.color = color[0];
        document.getElementById("pw_ch_button").disabled = true;
    }
}
function send() {
    var ch_pw_1 = document.getElementById("ch_pw_1").value,
     ch_pw_2 = document.getElementById("ch_pw_2").value;
    if (ch_pw_1 != ch_pw_2) {
        alert(pass_doesnt_match_msg);
        return;
    }
    var params = 'inquiry=' + lng_k_email_inquiry + '&pw=' + document.getElementById('ch_pw_1').value + '&uid=' + str_User_id;
    dhtmlxAjax.post("change_pw.asp", params, function(loader) {
        var resp = loader.xmlDoc.responseText;
        if (resp.substr(0, 1) == '{') {
            var json = eval("(" + resp + ")");
            if (json && typeof json === 'object') {
                if (json.status == 'error') {
                    alert("Error: " + json.msg);
                } else {
                    if (json.msg != '' && json.msg != undefined) {
                        ch_pw.hide();
                        document.getElementById('lost_pw_msg_text').innerHTML = json.msg;
                        $('#lost_pw_msg_b').click();
                    }
                }
            } else {
                alert('Unknown request error occured');
            }
        } else {
            alert('Error requesting the data. Error: ' + loader.xmlDoc.responseText);
        }
    });
}
function in_array(needle, haystack, argStrict) {
    var found = false, key, strict = !!argStrict;
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
    return found;
}
function show() {
    if (document.getElementById('ch_pw_bottom2').style.display == 'none') {
        $(".ch_pw_bottom2").show();
        document.getElementById('simplemodal-container').style.height = "410px"
    } else {
        $(".ch_pw_bottom2").hide();
        document.getElementById('simplemodal-container').style.height = "228px";
    }
}
//p.brislinger@inode.at